from enum import Enum, unique


class BaseEnum(Enum):
    def __new__(cls, key, desc):
        obj = object.__new__(cls)
        obj.key = obj._value_ = key
        obj.desc = desc
        return obj

    @classmethod
    def get_enum(cls, value):
        try:
            return cls(value)
        except ValueError:
            return None

    @classmethod
    def choices(cls):
        return tuple([(item.key, item.desc) for item in cls])

    @classmethod
    def items(cls):
        return tuple([item.key for item in cls])


@unique
class OrganizationState(BaseEnum):
    INIT = 'init', '初始状态'
    AUDIT_PENDING = 'audit_pending', '已提交审核，待审核'
    AUDIT_PASSED = 'audit_passed', '审核通过'
    AUDIT_REJECTED = 'audit_rejected', '未审核通过'
    PAID = 'paid', '已付费'


@unique
class OrganizationNature(BaseEnum):
    PRIVATE = 'private', '私营'
    STATE_OWNED = 'state_owned', '国有'
    JOINT_VENTURES = 'joint_ventures', '合资'
    FOREIGN_FUNDED = 'foreign_funded', '外资'


@unique
class OrganizationScale(BaseEnum):
    SCALE_UNDER_20 = '<20', '20人以下'
    SCALE_20_100 = '20-100', '20~100人'
    SCALE_100_500 = '100-500', '100~500人'
    SCALE_500 = '>500', '500人以上'


@unique
class Permission(BaseEnum):
    CONNECTION_VIEW = 'connection-view', '查看互联'
    CONNECTION_MANAGE = 'connection-manage', '互联管理'
    TEMPLATE_VIEW = 'template-view', '查看模版'
    TEMPLATE_MANAGE = 'template-manage', '模版管理'
    MODEL_VIEW = 'model-view', '查看模型'
    MODEL_MANAGE = 'model-manage', '管理模型'

    DATA_CENTER_VIEW = 'data-center-view', '查看数据中心'
    DATA_CENTER_SQL = 'data-center-sql', 'SQL操作（谨慎使用）'

    ORGANIZATION_VIEW = 'organization-view', '查看企业'
    ORGANIZATION_MANAGE = 'organization-manage', '企业管理'

    TASK_VIEW = 'task-view', '查看任务'
    TASK_MANAGE = 'task-manage', '任务管理'

    APP_MANAGE = 'app-manage', '应用管理'

    ORGANIZATION_INFO_MANAGE = 'organization-info-manage', '企业信息'
    USER_MANAGE = 'user-manage', '用户管理'
    ROLE_MANAGE = 'role-manage', '角色管理'
    ENCRYPT_MANAGE = 'encrypt-manage', '密钥管理'
    OPERATION_LOG_VIEW = 'operation-log-view', '查看操作记录'


@unique
class SystemAppKey(BaseEnum):
    WWEXX = 'wwexx', 'WWEXX'


@unique
class Authorizer(BaseEnum):
    OUR_SIDE = 'our_side', '我方'
    OTHER_SIDE = 'other_side', '对方'


@unique
class ConnectionState(BaseEnum):
    DRAFT = 'draft', '草稿'
    ONGOING = 'ongoing', '进行中'
    CANCELLED = 'cancelled', '已作废'
    REJECTED = 'rejected', '已拒绝'


@unique
class PartnerType(BaseEnum):
    ORG = 'org', '企业'
    APP = 'app', '应用'


class LogType(BaseEnum):
    CREATE = 'create', '创建'
    DELETE = 'delete', '删除'
    UPDATE = 'update', '修改'


class LogModule(BaseEnum):
    APP_CONNECTION = 'app_connection', '应用互联'
    ORG_CONNECTION = 'org_connection', '企业互联'
    MODEL = 'model', '模型管理'
    USER = 'user', '用户管理'
    ROLE = 'role', '角色管理'
    CERT_TEMPLATE = 'certificate_template', '授权书模版'
    ENCRYPT = 'encrypt', '密钥管理'
    TASK = 'task', '任务管理'


class PluginInstallState(BaseEnum):
    PENDING = 'pending', '等待中'
    INSTALLING = 'installing', '安装中'
    INSTALLED = 'installed', '已安装'
    INSTALL_FAILED = 'install_failed', '安装失败'
    UNINSTALLING = 'uninstalling', '卸载中'
    UNINSTALL_FAILED = 'uninstall_failed', '卸载失败'
